package session.stateless.FRM;

import entity.FRM.CreditAccount;
import entity.CRM.Customer;
import entity.CRM.SaleOrder;
import entity.FRM.CashInAdvance;
import entity.FRM.CustomerInvoice;
import entity.FRM.CustomerPayment;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class CreditAccountBean implements CreditAccountBeanLocal {
    @PersistenceContext 
    private EntityManager em; 
    
    @Override
    public CreditAccount createCreditAccount() {
        CreditAccount creditAccount = new CreditAccount();
        creditAccount.setCreditLimit(new Double(5000)); //default 5000
        creditAccount.setCreditDiscount(new Double(2)); //default 2% if within credit period 
        creditAccount.setCreditPeriod(new Long(10));    //default 10 days since invoice is created
        creditAccount.setNetPeriod(new Long(30));       //default 30 days since invoice is created
        creditAccount.setDeposit(new Double(0));        //No deposit made
        creditAccount.setOutstandingCredit(new Double(0));
        
        em.persist(creditAccount);
        return creditAccount;
    }
    
    public void addDeposit(String registrationNumber, Double depositAdd) {
        Customer customer = em.find(Customer.class, registrationNumber);
        CreditAccount creditAccount = customer.getCreditAccount();
        creditAccount.setDeposit((creditAccount.getDeposit()+depositAdd));
        creditAccount.setCreditLimit(creditAccount.getCreditLimit()+2*depositAdd);
        em.flush();        
    }

    public void modifyCreditAccount_AddConfirmedSaleOrder(Long saleOrderId) {
        SaleOrder saleOrder = em.find(SaleOrder.class, saleOrderId);
        Customer customer = saleOrder.getCustomer();
        CreditAccount creditAccount = customer.getCreditAccount();
        
        if ((saleOrder.getCashInAdvance()==null)|| (saleOrder.getCashInAdvance().getStateCashInAdvance()==CashInAdvance.StateCashInAdvance.Pending))
        {
            creditAccount.setOutstandingCredit(creditAccount.getOutstandingCredit()+saleOrder.getTotalPrice());        
        }
        else
            creditAccount.setOutstandingCredit(creditAccount.getOutstandingCredit()+saleOrder.getTotalPrice()-saleOrder.getCashInAdvance().getCashInAdvanceAmount());
        em.flush();
    }
    
    @Override
    public void modifyCreditAccount_AddCustomerPayment(Long customerPaymentId) {    
        CustomerPayment customerPayment = em.find(CustomerPayment.class, customerPaymentId);
        CustomerInvoice customerInvoice = customerPayment.getCustomerInvoice();
        CreditAccount creditAccount = customerInvoice.getSaleOrder().getCustomer().getCreditAccount();
        
        creditAccount.setOutstandingCredit(creditAccount.getOutstandingCredit()- customerInvoice.getCustomerInvoiceAmount());
        
        em.flush();
    }
    
    public Double isPass_checkCredit(Long saleOrderId) {
        SaleOrder saleOrder = em.find(SaleOrder.class, saleOrderId);
        Customer customer = saleOrder.getCustomer();
        
        CreditAccount creditAccount = customer.getCreditAccount();
        if ((saleOrder.getCashInAdvance()==null)|| (saleOrder.getCashInAdvance().getStateCashInAdvance()==CashInAdvance.StateCashInAdvance.Pending))
        {            
            return creditAccount.getCreditLimit()-(creditAccount.getOutstandingCredit()+saleOrder.getTotalPrice());        
        }
        else            
            return creditAccount.getCreditLimit()-(creditAccount.getOutstandingCredit()+saleOrder.getTotalPrice()-saleOrder.getCashInAdvance().getCashInAdvanceAmount());                    
    }

    

    

    
}
